package dailyhealth.service;

import dailyhealth.controller.vo.DailyHealthVO;
import dailyhealth.controller.vo.ResponseDataVO;
import org.springframework.stereotype.Service;
import util.ErrorCode;
import util.ResponseUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * @author: cjn
 * @date: 2021/2/8 22:14
 */
@Service
public class DailyHealthService {

    //体温3，呼吸率2，心率2，血压3
    public double calcScoreByLifeSigns(double temperature, double respiratoryRate, double heartRate, String bloodPressure) {
        double score = 0.0;
        //体温异常 35.1~37.2为正常
        if (temperature < 35.1 || temperature > 37.2) {
            score += 3;
        }
        //呼吸率异常 10~28为正常
        if (respiratoryRate < 10 || respiratoryRate > 28) {
            score += 2;
        }
        //心率异常 60~100为正常
        if (heartRate < 60 || heartRate > 100) {
            score += 2;
        }
        //血压异常
        String[] temp = bloodPressure.split("/");
        double systolicPressure = Double.parseDouble(temp[0]); //90~140
        double diastolicPressure = Double.parseDouble(temp[1]); //50~90
        //低血压，高血压
        if ((systolicPressure < 90 || diastolicPressure < 50) || (systolicPressure > 140 || diastolicPressure > 90)) {
            score += 3;
        }
        return score;
    }

    //饮食2，睡眠2 心情2 运动1 小便1 大便2
    public double calcScoreByDailyHabits(String diet, String sleep, String mood, String sports, String urine, String bowel) {
        double score = 0.0;
        //饮食
        if (!diet.equals("如常")) {
            score += 2;
        }
        //睡眠
        if (!sleep.equals("睡眠好")) {
            score += 2;
        }
        //心情
        if (!mood.equals("好")) {
            score += 2;
        }
        //运动
        if (!sports.equals("如常")) {
            score += 1;
        }
        //小便
        if (!urine.equals("如常")) {
            score += 1;
        }
        //大便
        if (!bowel.equals("如常")) {
            score += 2;
        }
        return score;
    }

    //服药情况1，意外改变5，病情变化3，意识变化10
    public double calcScoreByOthers(String medication, String accidents, String illnessChange, String consciousnessChange) {
        int score = 0;
        //服药情况（选项：无需服药、正常服药、未按医嘱服药）
        if (medication.equals("未按医嘱服药")) {
            score += 1;
        }
        //意外改变
        if (!accidents.equals("无")) {
            score += 5;
        }
        //病情变化 (选项：没有疾病、平稳、有变化)
        if (illnessChange.equals("有变化")) {
            score += 3;
        }
        return score;
    }

    //计算总得分并给出对应的建议
    public String getScoreAndSuggestion(DailyHealthVO dailyHealthVO) {
        double byLifeSigns = calcScoreByLifeSigns(dailyHealthVO.getTemperature(), dailyHealthVO.getRespiratoryRate(), dailyHealthVO.getHeartRate(), dailyHealthVO.getBloodPressure());
        double byDailyHabits = calcScoreByDailyHabits(dailyHealthVO.getDiet(), dailyHealthVO.getSleep(), dailyHealthVO.getMood(), dailyHealthVO.getSports(), dailyHealthVO.getUrine(), dailyHealthVO.getBowel());
        double byOthers = calcScoreByOthers(dailyHealthVO.getMedication(), dailyHealthVO.getAccidents(), dailyHealthVO.getIllnessChange(), dailyHealthVO.getConsciousnessChange());
        double score = byLifeSigns + byDailyHabits + byOthers;
//        String msuggestion = "您的得分是" + score + "分。\n";
        ResponseDataVO responseDataVO = new ResponseDataVO();
        responseDataVO.setScore(score);
        // 2 5 10 15
        if (score == 0) {
            // 0
//            msuggestion = "异常情况得分0分，目前身体状况平稳，保持现状。";
            responseDataVO.setConclusion("异常情况得分0分，目前身体状况平稳，保持现状。");
        } else if (score <= 2) {
            //(0,2]
//            msuggestion += "建议：身体状态较好，暂无建议。";
//            msuggestion += "建议：注意休息，调整生活节律。";
            responseDataVO.setConclusion("注意休息，调整生活节律。");
//        } else if (score == 3) {
//            // 3
//            msuggestion += "建议：注意休息，调整活动、饮食、睡眠，注意观察血压、心率和体温的异常变化。";
        } else if (score <= 5) {
            // (2,5]
//            msuggestion += "建议：注意休息和生活调整。";
//            msuggestion += "建议：注意休息，调整活动、饮食、睡眠，注意观察血压、心率和体温的异常变化。";
            responseDataVO.setConclusion("注意休息，调整活动、饮食、睡眠，注意观察血压、心率和体温的异常变化。");
        } else if (score <= 10) {
            //(5, 10]
//            msuggestion += "建议：身体状态有变化，需密切观察，每日评估，向院长汇报。";
            responseDataVO.setConclusion("身体状态有变化，需密切观察，每日评估，向院长汇报。");
        } else if (score <= 15) {
            //(10, 15]
//            msuggestion += "建议：有病情变化或加重，及时向社区医生报告，做相应检查和判断。";
            responseDataVO.setConclusion("有病情变化或加重，及时向社区医生报告，做相应检查和判断。");
        } else {
            //(15, 999)
//            msuggestion += "建议：病情严重，立刻急诊就医。";
            responseDataVO.setConclusion("病情严重，立刻急诊就医。");
        }
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "查询成功", responseDataVO);
    }


}
